#define _WIN32_DCOM

#include <windows.h>
#include <iostream.h>
#include <stdio.h>

int main(int argc, char* argv[])
{
	CoInitializeEx(NULL, COINIT_MULTITHREADED);

	VARIANT v1;
	VARIANT v2;

	VariantInit(&v1);
	VariantInit(&v2);

	v1.vt = VT_I4;
	v1.lVal = 5;

	HRESULT hr = VariantChangeType(&v2, &v1, 0, VT_BSTR);
	if(SUCCEEDED(hr))
		wprintf(L"%s\n", v2.bstrVal);

	VariantClear(&v2);

// ----------

	BSTR b1;
	b1 = SysAllocString(L"Testing BSTRs");

	wprintf(L"%s\n", b1);
	wprintf(L"%d bytes\n", SysStringByteLen(b1));
	wprintf(L"%d characters\n", SysStringLen(b1));

	SysFreeString(b1);

// ----------

	long myArray[2][4];
	myArray[1][3] = 3;

	SAFEARRAYBOUND pSab[2];
	pSab[0].lLbound = 0;
	pSab[0].cElements = 4;
	pSab[1].lLbound = 0;
	pSab[1].cElements = 4;

	SAFEARRAY* pSa;
	pSa = SafeArrayCreate(VT_I4, 2, pSab);
	if(pSa == NULL)
		cout << "SafeArrayCreate failed" << endl;

	long index[2] = { 2, 1 };
	long data = 3;
	SafeArrayPutElement(pSa, index, &data);

	long NewData;
	SafeArrayGetElement(pSa, index, &NewData);
	cout << NewData << endl;

	long* pData;
	SafeArrayAccessData(pSa, (void**)&pData);

	*pData = 4;
	*(pData + 1) = 5;
	*(pData + 2) = 6;
	*(pData + 3) = 7;

	*(pData + 4) = 8;
	*(pData + 5) = 9;
	*(pData + 6) = 10;
	*(pData + 7) = 11;

	SafeArrayUnaccessData(pSa);

	index[0] = 3;
	index[1] = 1;
	SafeArrayGetElement(pSa, index, &NewData);
	cout << NewData << endl;

	VARIANT v3;
	VariantInit(&v3);

	v1.vt = VT_I4|VT_ARRAY;
	v1.parray = pSa;

	SafeArrayDestroy(pSa);

	CoUninitialize();
	return 0;
}